package com.toppitch.site.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MailUtils {
	@Autowired
	private MailConfigBean mailConfig;
	@Autowired
	private GmailConfigBean gmailConfig;

	public void send(String toAddress, String subject, String content) {
		Session sendMailSession = Session.getDefaultInstance(mailConfig.getProperties(), mailConfig);
		try {
			Message mailMessage = new MimeMessage(sendMailSession);
			Address from = new InternetAddress(mailConfig.getFromAddress());
			mailMessage.setFrom(from);
			Address to = new InternetAddress(toAddress);
			mailMessage.setRecipient(Message.RecipientType.TO, to);
			mailMessage.setSubject(subject);
			mailMessage.setSentDate(new Date());
			Multipart mainPart = new MimeMultipart();
			BodyPart html = new MimeBodyPart();
			html.setContent(content, "text/html; charset=utf-8");
			mainPart.addBodyPart(html);
			mailMessage.setContent(mainPart);
			Transport.send(mailMessage);
		} catch (Throwable ex) {
			ex.printStackTrace();
		}
	}

	public void sendGmail(String toAddress, String subject, String content) {

		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", gmailConfig.getHost());
		props.setProperty("mail.smtp.socketFactory.class", gmailConfig.getSocketFactoryClass());
		props.setProperty("mail.smtp.socketFactory.fallback", gmailConfig.getSocketFactoryFallback());
		props.setProperty("mail.smtp.port", gmailConfig.getPort());
		props.setProperty("mail.smtp.socketFactory.port", gmailConfig.getSocketFactoryPort());
		props.setProperty("mail.smtp.auth", gmailConfig.getAuth());
		props.setProperty("mail.smtp.ssl", gmailConfig.getSsl());

		try {
			Session session = Session.getDefaultInstance(props, new Authenticator() {
				public PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(gmailConfig.getUserName(), gmailConfig.getPassword());
				}
			});

			// create a new message
			Message message = new MimeMessage(session);

			message.setFrom(new InternetAddress(gmailConfig.getFrom()));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
			message.setSubject(subject);
			Multipart multipart = new MimeMultipart();
			BodyPart bodyPart = new MimeBodyPart();
			bodyPart.setContent(content, gmailConfig.getMailContentType());
			multipart.addBodyPart(bodyPart);
			message.setContent(multipart);
			message.setSentDate(new Date());

			Transport.send(message);
		} catch (Throwable ex) {
			ex.printStackTrace();
		}
	}

}
